﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using LuaInterface;
using System.Reflection;
using ElementumForever.Utility;
using ElementumForever.Editer;
using ElementumForever.Interfaces;

namespace ElementumForever.Factory
{
    public class LuaFactory
    {
        private static Dictionary<string, BaseEntity> prototypes;

        public static void Initialize()
        {
            prototypes = new Dictionary<string, BaseEntity>();
            LuaFactory.AddPrototype(new RectangleShape());
        }

        private static void AddPrototype(string id, BaseEntity prototype)
        {
            prototypes.Add(id, prototype);
        }

        private static void AddPrototype(BaseEntity prototype)
        {
            prototypes.Add(prototype.ObjectClass.ToString(), prototype);
        }

        private static BaseEntity GetPrototype(string id)
        {
            if (!prototypes.ContainsKey(id))
                throw new Exception("prototype:" + id + "not exist");
            return (BaseEntity)prototypes[id].Clone();
        }

        public static BaseEntity CreatObject(LuaTable table)
        {
            return CreatObject((string)table[LuaConstants.Name],
                (string)table[LuaConstants.ObjectClass],
                Convert.ToSingle(table[LuaConstants.Width]), Convert.ToSingle(table[LuaConstants.Height]),
                Convert.ToSingle(table[LuaConstants.OriginX]), Convert.ToSingle(table[LuaConstants.OriginY]),
                Convert.ToSingle(table[LuaConstants.ScaleX]), Convert.ToSingle(table[LuaConstants.ScaleY]),
                Convert.ToSingle(table[LuaConstants.X]), Convert.ToSingle(table[LuaConstants.Y]),
                Convert.ToSingle(table[LuaConstants.Rotation]),
                UnitHelper.ToColor((LuaTable)table[LuaConstants.Color]));
        }

        public static BaseEntity CreatObject(string name, string _class,
            float width, float height,
            float OriginX, float OriginY,
            float scale,
            float positionX, float positionY,
            float rotation,
            Color color)
        {
            return CreatObject(name, _class, width, height, OriginX, OriginY, scale, scale, positionX, positionY, rotation, color);
        }

        public static BaseEntity CreatObject(string name, string _class,
            float width, float height,
            float originX, float originY,
            float scaleX, float scaleY,
            float positionX, float positionY,
            float rotation,
            Color color)
        {
           
            BaseEntity _object;

            _object = GetPrototype(_class);
            _object.OnCreat();
            _object.Name = name;
            _object.Width=width;
            _object.Height=height;
            _object.OriginX=originX;
            _object.OriginY=originY;
            _object.X = positionX;
            _object.Y = positionY;
            _object.Color = color;
            _object.ScaleX = scaleX;
            _object.ScaleY = scaleY;
            _object.Rotation = rotation;
            return _object;
        }

        private static string ToLuaCode(EntityInfo data)
        {
            string val = data.Name + "={";

            foreach (EntityInfo _data in data.Table.Values)
            {
                if (_data.IsTable)
                    val += ("\n" + ToLuaCode(_data) + ",");
                else
                {
                    val += ("\n" + _data.ToString() + ",");
                }
            }
            val += "\n}";
            return val;
        }

        public static string ToLuaCodeString(EntityInfo data)
        {
            string val = "Scene" + "={";

            foreach (EntityInfo _data in data.Table.Values)
            {
                if (_data.IsTable)
                    val += ("\n" + ToLuaCode(_data) + ",");
                else
                {
                    val += ("\n" + _data.ToString() + ",");
                }
            }
            val += "\n}";
            return val;
        }

    }
}
